package JVM.philosper;

public class philosper extends Thread{
    int num;
    chopstick l=null;
    chopstick r=null;
    public  philosper(int num,chopstick c1,chopstick c2){
        this.num=num;
        this.l=c1;
        this.r=c2;
    }
    @Override
    public void run() {
        if (num%2==0){
            synchronized (r){
                try {
                    sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (l){
                    try {
                        sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(num+"   eatting----");
                }
            }
        }else {
            synchronized (l) {
                try {
                    sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (r) {
                    try {
                        sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(num + "   eatting----");
                }
            }
        }
    }

    public static void main(String[] args) {
        chopstick c1=new chopstick(1);
        chopstick c2=new chopstick(2);
        chopstick c3=new chopstick(3);
        chopstick c4=new chopstick(4);
        chopstick c5=new chopstick(5);
        philosper p1=new philosper(1,c1,c2);
        philosper p2=new philosper(2,c2,c3);
        philosper p3=new philosper(3,c3,c4);
        philosper p4=new philosper(4,c4,c5);
        philosper p5=new philosper(5,c5,c1);
        p1.start();
        p2.start();
        p3.start();
        p4.start();
        p5.start();
    }
}
